package com.liang.springcloud.controller;

import com.liang.springcloud.entities.CommonResult;
import com.liang.springcloud.entities.Payment;
import com.liang.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.support.FallbackCommand;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @description:
 * @author: 史德亮
 * @time: 2020/11/10 10:59
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "global_fallback")
public class PaymentController {
    @Resource
    private PaymentService paymentService;



    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        return paymentService.getPaymentById(id);
    }

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String payment_ok(@PathVariable("id") Integer id){
        return paymentService.payment_ok(id);
    }

//    @HystrixCommand(fallbackMethod = "order_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
//    })
    @HystrixCommand //使用默认兜底
    @GetMapping("/consumer/payment/hystrix/timeOut/{id}")
    public String payment_timeOut(@PathVariable("id") Integer id){

//        try {
//            Thread.sleep(300);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return paymentService.payment_timeOut(id);
    }

    /**
     * 特殊兜底方案，超时时调用
     * @return
     */
    private String order_TimeOutHandler(Integer id){
        return "线程池:" + Thread.currentThread().getName() + "order服务 特殊 系统繁忙或运行错误,请稍后重试,id:"  + "\t" + "o(╥﹏╥)o";
    }

    /**
     * 默认兜底方案，超时时调用
     * @return
     */
    private String global_fallback(){
        return "线程池:" + Thread.currentThread().getName() + "order服务 默认兜底方法 系统繁忙或运行错误,请稍后重试,id:"  + "\t" + "o(╥﹏╥)o";
    }


}
